/*********************************************************************************
Organization 					: 				Georgia Institute of Technology
  
Institute 						:				Cognitive Computing Group(CCL)
 
Authors							: 				Santiago Ontanon
 												
Class							:				EpisodicCaseBase
 
Function						: 				
*********************************************************************************/

package cbr.episodicCBR;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

import util.XMLWriter;

// XXX no CaseBase parent class?
public class EpisodicCaseBase {
	public List<CaseBaseBehavior> m_CB_behaviors;
	public List<CaseBaseEpisode> m_CB_episodes;
	
	public EpisodicCaseBase()
	{
		m_CB_behaviors = new LinkedList<CaseBaseBehavior>();
		m_CB_episodes = new LinkedList<CaseBaseEpisode>();		
	}
	
	public void addBehavior(CaseBaseBehavior b) {
		m_CB_behaviors.add(b);		
	}
	
	public void addEpisode(CaseBaseEpisode e) {
		m_CB_episodes.add(e);		
	}	
	
	public void loadCaseBaseFromXML(String fileNameBehaviors, String fileNameEpisodes)
	throws Exception {
		// No validation because I'm too lazy to write and maintain a schema.
		SAXBuilder sb = new SAXBuilder(false); 
		Document jdoc = sb.build(fileNameBehaviors);
		
		assert jdoc.getRootElement().getName().equals("behaviorlibrary") :
			"EpisodicCaseBase.loadCaseBaseFromXML: Invalid Behavior XML root" + 
			jdoc.getRootElement().getName();
		
		Element e;
		CaseBaseBehavior cbb;
		for (Object o : jdoc.getRootElement().getChildren("behavior")) {
			e = (Element) o;
			System.out.println("EpisodicCaseBase: found behavior ");
			cbb = CaseBaseBehavior.loadFromXML(e); 
			m_CB_behaviors.add(cbb);
			System.out.println("EpisodicCaseBase: loaded behavior " + cbb.m_ID);
		}
		
		
		// No validation because I'm too lazy to write and maintain a schema.
		sb = new SAXBuilder(false); 
		jdoc = sb.build(fileNameEpisodes);
		
		assert jdoc.getRootElement().getName().equals("episodelibrary") :
			"EpisodicCaseBase.loadCaseBaseFromXML: Invalid Episode XML root" + 
			jdoc.getRootElement().getName();
		
		CaseBaseEpisode cbe;
		for (Object o : jdoc.getRootElement().getChildren("episode")) {
			e = (Element) o;
			System.out.println("EpisodicCaseBase: found episode ");
			cbe = CaseBaseEpisode.loadFromXML(e); 
			m_CB_episodes.add(cbe);
			System.out.println("EpisodicCaseBase: loaded episode" + cbe.m_behaviorID);
		}
	}	
	
	public void printSummary() {
		
		System.out.println("Summary of the Case Base:");
		for(CaseBaseBehavior b:m_CB_behaviors) {
			System.out.println("Behavior " + b.m_ID + ":");
			System.out.println(b.getGoal());
			System.out.println(b.getPlan());
			System.out.println("Episodes:");
			for(CaseBaseEpisode e:m_CB_episodes) {
				if (e.m_behaviorID.equals(b.m_ID)) {
					System.out.println("Outcome: " + e.m_outcome);
				}
			}
			System.out.println("");
		}
	}
	

	public void saveCaseBaseToXML(String fileNameBehaviors, String fileNameEpisodes) throws IOException {
		XMLWriter w = new XMLWriter(new PrintWriter(new FileWriter(fileNameBehaviors)));

		w.rawXML("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
		w.tag("behaviorlibrary xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
			"  xsi:noNamespaceSchemaLocation=\"Wargus-behaviorbase.xsd\""); 
		for (CaseBaseBehavior b : m_CB_behaviors) b.saveToXML(w);
		w.tag("/behaviorlibrary");	
		w.close();		
		
		w = new XMLWriter(new PrintWriter(new FileWriter(fileNameEpisodes)));

		w.rawXML("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
		w.tag("episodelibrary xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
			"  xsi:noNamespaceSchemaLocation=\"Wargus-episodebase.xsd\"");
		for (CaseBaseEpisode e : m_CB_episodes) e.saveToXML(w);
		w.tag("/episodelibrary");
		w.close();		
	}	
}
